package com.usian.controller;

import com.usian.car.Car;
import com.usian.entity.Result;
import com.usian.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Author:张永杰
 * @ClassName: CarControlelr
 * @Date: 2024/12/11
 * @Description:
 */
@RestController
@RequestMapping("/car")
public class CarControlelr {
    @Autowired
    private CarService carService;

    /**
     * 查询所有车辆
     * @return
     */
    @GetMapping("findAllCar")
    public Result<List<Car>> findAAllCar(){
        Result<List<Car>> allCar = carService.findAllCar();
        return allCar;
    }

    @PostMapping("uploadCar")
    public String uploadCar(@RequestParam("file") MultipartFile file){
        //获取文件名称
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + suffix;
        try {
            String url = carService.uploadCar(file.getBytes(),newFileName);
            return url;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
